package com.cognizant.ams.controller.user;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cognizant.ams.bean.Lduser;
import com.cognizant.ams.common.PageHelper;
import com.cognizant.ams.service.ComlistService;
import com.cognizant.ams.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;	
	@Autowired
	private ComlistService comlistService;	
	
	@GetMapping("/getUser")
	public ModelAndView getUser(String usercode,String username,String comcode,String cur,String op) {
		System.out.println("usercode===="+usercode+",username=="+username+",comcode==="+comcode+"cur==="+cur+"===op=="+op);
		//获取根公司列表
		int pageCount=0;
		int pageSize=10;
		
		if (cur==null) {
			cur="1";
		}
		if (op!=null&&usercode!=null) {  //表示非查询操作，即删除，更新，添加等
			Lduser uLduser=new Lduser();
			uLduser.setUsercode(usercode);
			uLduser.setUsername(username);
			uLduser.setComcode(comcode);
			userService.dealOp(op, uLduser);
			//userService.adduser(usercode, username, comcode);
		}
		List<Map<String, Object>> list =userService.getUser(usercode, username, comcode);
		
		if (list!=null&&list.size()>0) {
			pageCount=list.size()/pageSize+1;
			list=PageHelper.getDataByFenye(list, cur,pageSize);
		}
		else {
			cur="0";
		}
		List<Map<String, Object>> comlist =comlistService.getComlist();
		ModelAndView modelAndView =new ModelAndView("user/user");
		modelAndView.addObject("usercode", usercode);
		modelAndView.addObject("username", username);
		modelAndView.addObject("comcode", comcode);
		modelAndView.addObject("users", list);
		modelAndView.addObject("comlist", comlist);
		modelAndView.addObject("pageCount", pageCount);
		modelAndView.addObject("cur", Integer.parseInt(cur));
		return modelAndView;
	}
	
	@PostMapping("/adduser")
	public ModelAndView adduser(String usercode,String username,String comcode ) {
		//int addresult =userService.adduser(usercode, username, comcode);
		ModelAndView modelAndView =new ModelAndView("user/user");
	//	modelAndView.addObject("addresult", addresult);
	
		return modelAndView;

	}
	 
}
